package org.efs.openreports.engine.sqlsupport;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class StatementWrapper implements Statement {
    protected static final Logger LOG = Logger.getLogger( StatementWrapper.class );
    private Statement delegate;
    private ConnectionWrapper connectionWrapper;
    private static int statementCount = 0;
    private int statementNumber;
    private boolean closeWasCalled = false;
    
    public boolean closeWasCalled() {
        return closeWasCalled;
    }
    
    public StatementWrapper(ConnectionWrapper connectionWrapper, Statement delegate) {
        this.delegate = delegate;
        this.connectionWrapper = connectionWrapper;
        this.statementNumber =  ++statementCount;
        log( "Constructor");
    }
    
    protected void log(String message) {
        LOG.info( "MJCONNER StatementWrapper: " + connectionWrapper.getConnectionNumber() + "." + getStatementNumber() + ": " + message);
    }

    private int getStatementNumber() {
        return statementNumber;
    }

    public void addBatch( String sql ) throws SQLException {
        log( "addBatch: " + sql);
        delegate.addBatch( sql );
    }

    public void cancel() throws SQLException {
        delegate.cancel();
    }

    public void clearBatch() throws SQLException {
        delegate.clearBatch();
    }

    public void clearWarnings() throws SQLException {
        delegate.clearWarnings();
    }

    public void close() throws SQLException {
        log( "close(): closing..." );
        delegate.close();
        closeWasCalled  = true;
        log( "close(): closed" );
    }

    public boolean execute( String sql, int autoGeneratedKeys ) throws SQLException {
        log( "execute(String sql, int autoGeneratedKeys) : " + sql );
        return delegate.execute( sql, autoGeneratedKeys );
    }

    public boolean execute( String sql, int[] columnIndexes ) throws SQLException {
        log( "execute(String sql, int[] columnIndexes ) : " + sql );
        return delegate.execute( sql, columnIndexes );
    }

    public boolean execute( String sql, String[] columnNames ) throws SQLException {
        log( "execute(String sql, String[] columnIndexes ) : " + sql );
        return delegate.execute( sql, columnNames );
    }

    public boolean execute( String sql ) throws SQLException {
        log( "execute(String sql ) : " + sql );
        return delegate.execute( sql );
    }

    public int[] executeBatch() throws SQLException {
        log( "executeBatch()" );
        return delegate.executeBatch();
    }

    public ResultSet executeQuery( String sql ) throws SQLException {
        log( "executeQuery(String sql): " + sql );
        return delegate.executeQuery( sql );
    }

    public int executeUpdate( String sql, int autoGeneratedKeys ) throws SQLException {
        log( "executeUpdate(String sql, int autoGeneratedKeys): " + sql );
        return delegate.executeUpdate( sql, autoGeneratedKeys );
    }

    public int executeUpdate( String sql, int[] columnIndexes ) throws SQLException {
        log( "executeUpdate(String sql, int[] columnIndexes ): " + sql );
        return delegate.executeUpdate( sql, columnIndexes );
    }

    public int executeUpdate( String sql, String[] columnNames ) throws SQLException {
        log( "executeUpdate(String sql, String[] columnNames ): " + sql );
        return delegate.executeUpdate( sql, columnNames );
    }

    public int executeUpdate( String sql ) throws SQLException {
        log( "executeUpdate(String sql ): " + sql );
        return delegate.executeUpdate( sql );
    }

    public Connection getConnection() throws SQLException {
        return delegate.getConnection();
    }

    public int getFetchDirection() throws SQLException {
        return delegate.getFetchDirection();
    }

    public int getFetchSize() throws SQLException {
        return delegate.getFetchSize();
    }

    public ResultSet getGeneratedKeys() throws SQLException {
        return delegate.getGeneratedKeys();
    }

    public int getMaxFieldSize() throws SQLException {
        return delegate.getMaxFieldSize();
    }

    public int getMaxRows() throws SQLException {
        return delegate.getMaxRows();
    }

    public boolean getMoreResults() throws SQLException {
        return delegate.getMoreResults();
    }

    public boolean getMoreResults( int current ) throws SQLException {
        return delegate.getMoreResults( current );
    }

    public int getQueryTimeout() throws SQLException {
        return delegate.getQueryTimeout();
    }

    public ResultSet getResultSet() throws SQLException {
        return delegate.getResultSet();
    }

    public int getResultSetConcurrency() throws SQLException {
        return delegate.getResultSetConcurrency();
    }

    public int getResultSetHoldability() throws SQLException {
        return delegate.getResultSetHoldability();
    }

    public int getResultSetType() throws SQLException {
        return delegate.getResultSetType();
    }

    public int getUpdateCount() throws SQLException {
        return delegate.getUpdateCount();
    }

    public SQLWarning getWarnings() throws SQLException {
        return delegate.getWarnings();
    }

    public boolean isClosed() throws SQLException {
        return delegate.isClosed();
    }

    public boolean isPoolable() throws SQLException {
        return delegate.isPoolable();
    }

    public boolean isWrapperFor( Class<?> iface ) throws SQLException {
        return delegate.isWrapperFor( iface );
    }

    public void setCursorName( String name ) throws SQLException {
        delegate.setCursorName( name );
    }

    public void setEscapeProcessing( boolean enable ) throws SQLException {
        delegate.setEscapeProcessing( enable );
    }

    public void setFetchDirection( int direction ) throws SQLException {
        delegate.setFetchDirection( direction );
    }

    public void setFetchSize( int rows ) throws SQLException {
        delegate.setFetchSize( rows );
    }

    public void setMaxFieldSize( int max ) throws SQLException {
        delegate.setMaxFieldSize( max );
    }

    public void setMaxRows( int max ) throws SQLException {
        delegate.setMaxRows( max );
    }

    public void setPoolable( boolean poolable ) throws SQLException {
        delegate.setPoolable( poolable );
    }

    public void setQueryTimeout( int seconds ) throws SQLException {
        delegate.setQueryTimeout( seconds );
    }

    public <T> T unwrap( Class<T> iface ) throws SQLException {
        return delegate.unwrap( iface );
    }

}
